package org.example.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备实体类
 */
@Entity
@Table(name = "devices")
@EntityListeners(AuditingEntityListener.class)
public class Device {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @NotBlank(message = "设备名称不能为空")
    @Column(nullable = false, length = 100)
    private String name;
    
    @Column(name = "device_type_id", nullable = false)
    private Long deviceTypeId;
    
    @Column(name = "user_id", nullable = false)
    private Long userId;
    
    @Column(length = 50)
    private String room;
    
    @Column(nullable = false)
    private Integer status = 0; // 1-开启，0-关闭
    
    @Column(name = "is_online", nullable = false)
    private Integer isOnline = 1; // 1-在线，0-离线
    
    @Column(name = "ip_address", length = 45)
    private String ipAddress;
    
    @Column(name = "mac_address", length = 17)
    private String macAddress;
    
    @Column(columnDefinition = "JSON")
    private String properties; // JSON格式的设备属性
    
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
    
    // 多对一关系：设备类型
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "device_type_id", insertable = false, updatable = false)
    @JsonIgnore
    private DeviceType deviceType;
    
    // 多对一关系：所属用户
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", insertable = false, updatable = false)
    @JsonIgnore
    private User user;
    
    // 一对多关系：设备控制记录
    @OneToMany(mappedBy = "device", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private List<DeviceControlLog> controlLogs;
    
    // 一对多关系：场景设备关联
    @OneToMany(mappedBy = "device", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private List<SceneDevice> sceneDevices;
    
    // 构造函数
    public Device() {}
    
    public Device(String name, Long deviceTypeId, Long userId, String room) {
        this.name = name;
        this.deviceTypeId = deviceTypeId;
        this.userId = userId;
        this.room = room;
    }
    
    // Getter和Setter方法
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Long getDeviceTypeId() {
        return deviceTypeId;
    }
    
    public void setDeviceTypeId(Long deviceTypeId) {
        this.deviceTypeId = deviceTypeId;
    }
    
    public Long getUserId() {
        return userId;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    public String getRoom() {
        return room;
    }
    
    public void setRoom(String room) {
        this.room = room;
    }
    
    public Integer getStatus() {
        return status;
    }
    
    public void setStatus(Integer status) {
        this.status = status;
    }
    
    public Integer getIsOnline() {
        return isOnline;
    }
    
    public void setIsOnline(Integer isOnline) {
        this.isOnline = isOnline;
    }
    
    public String getIpAddress() {
        return ipAddress;
    }
    
    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }
    
    public String getMacAddress() {
        return macAddress;
    }
    
    public void setMacAddress(String macAddress) {
        this.macAddress = macAddress;
    }
    
    public String getProperties() {
        return properties;
    }
    
    public void setProperties(String properties) {
        this.properties = properties;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public DeviceType getDeviceType() {
        return deviceType;
    }
    
    public void setDeviceType(DeviceType deviceType) {
        this.deviceType = deviceType;
    }
    
    public User getUser() {
        return user;
    }
    
    public void setUser(User user) {
        this.user = user;
    }
    
    public List<DeviceControlLog> getControlLogs() {
        return controlLogs;
    }
    
    public void setControlLogs(List<DeviceControlLog> controlLogs) {
        this.controlLogs = controlLogs;
    }
    
    public List<SceneDevice> getSceneDevices() {
        return sceneDevices;
    }
    
    public void setSceneDevices(List<SceneDevice> sceneDevices) {
        this.sceneDevices = sceneDevices;
    }
}
